//
//  XMLoginViewController.m
//  XunMi
//
//  Created by MacBook on 2024/2/21.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMLoginViewController.h"
#import "XMLoginPrivacyTipView.h"
#import "XMPhoneLoginViewController.h"
#import <TencentOpenAPI/TencentOpenApiUmbrellaHeader.h>
#import <OneLoginSDK/OneLoginSDK.h>
#import "XMRegistOneViewController.h"

@interface XMLoginViewController () <XMWXApiManagerDelegate, TencentSessionDelegate>
@property (weak, nonatomic) IBOutlet UIButton *oneKeyButton;
@property (weak, nonatomic) IBOutlet UIButton *otherButton;
@property (weak, nonatomic) IBOutlet UIButton *weiButton;
@property (weak, nonatomic) IBOutlet UIButton *qqButton;
@property (weak, nonatomic) IBOutlet UIButton *agreeButton;
@property (nonatomic, strong) XMLoginPrivacyTipView *tipView;
@property (nonatomic, strong) TencentOAuth *tencentOAuth;
@property (nonatomic, assign) XMLoginType loginType;
@end

@implementation XMLoginViewController

- (void)rightAction {
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [kWXApiManager registerAppKey];
    kWXApiManager.delegate = self;
    
    [TencentOAuth setIsUserAgreedAuthorization:YES];
    self.tencentOAuth = [[TencentOAuth alloc] initWithAppId:kQQAppId andUniversalLink:kUNIVERSAL_LINK andDelegate:self];
    
//    [self.navRightBtn setTitle:@"先逛逛" forState:UIControlStateNormal];
//    [self.navRightBtn setTitleColor:kAppMainColor forState:UIControlStateNormal];
//    [self.navRightBtn addTarget:self action:@selector(rightAction) forControlEvents:UIControlEventTouchUpInside];
    
    [super viewDidLoad];
    self.navView.backgroundColor = UIColor.clearColor;
    self.navLeftBtn.hidden = YES;
    self.view.backgroundColor = UIColor.whiteColor;
    kCornerRadius(_oneKeyButton, 21, 0, UIColor.clearColor);
    kCornerRadius(_otherButton, 21, 0, UIColor.clearColor);
    
    [_weiButton setImagePosition:XMImagePositionTop spacing:1];
    [_qqButton setImagePosition:XMImagePositionTop spacing:1];
    [_agreeButton setImagePosition:XMImagePositionLeft spacing:2];
    
    XMWeakSelf(self);
    [self.tipView tipViewLeftBlock:^{
        [GKCover hideCover];
    } rightBlock:^{
        XMStrongSelf(self);
        self.agreeButton.selected = YES;
        XMWeakSelf(self);
        [GKCover hideCoverWithHideBlock:^{
            XMStrongSelf(self);
            switch (self.loginType) {
                case XMLoginTypeOneKey:
                {
                    [self oneKeyLogin];
                    
                }
                    break;
                case XMLoginTypeOther:
                {
                    [self phoneLogin];
                }
                    break;
                    
                case XMLoginTypeWei:
                {
                    [kWXApiManager weChatLogin];
                }
                    break;
                    
                case XMLoginTypeQQ:
                {
                    [self qqLogin];
                }
                    break;
                    
                default:
                    break;
            }
        }];
    }];
    
    self.tipView.protocolBlock = ^{
        XMStrongSelf(self);
        XMWebViewController *vc = [[XMWebViewController alloc] init];
        vc.titleStr = @"用户协议";
        vc.urlStr = kGlobalVariables.appConfig.privacy.serviceAgreement;
        vc.needNav = YES;
        [self.navigationController pushViewController:vc animated:YES];
    };
    
    self.tipView.privacyBlock = ^{
        XMStrongSelf(self);
        XMWebViewController *vc = [[XMWebViewController alloc] init];
        vc.titleStr = @"隐私政策";
        vc.urlStr = kGlobalVariables.appConfig.privacy.privacyAgreement;
        vc.needNav = YES;
        [self.navigationController pushViewController:vc animated:YES];
    };
    
    self.weiButton.hidden = ![WXApi isWXAppInstalled];
    self.qqButton.hidden= ![QQApiInterface isQQInstalled];
}

- (IBAction)oneAct:(UIButton *)sender {
    self.loginType = XMLoginTypeOneKey;
    if (!self.agreeButton.selected) {
        [self showTipView];
        return;
    }
    [self oneKeyLogin];
}

- (IBAction)otherAct:(UIButton *)sender {
    self.loginType = XMLoginTypeOther;
    if (!self.agreeButton.selected) {
        [self showTipView];
        return;
    }
    [self phoneLogin];
}

- (IBAction)agreementAct:(UIButton *)sender {
    XMWebViewController *vc = [[XMWebViewController alloc] init];
    vc.titleStr = @"用户协议";
    vc.urlStr = kGlobalVariables.appConfig.privacy.serviceAgreement;
    vc.needNav = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)privacyAct:(UIButton *)sender {
    XMWebViewController *vc = [[XMWebViewController alloc] init];
    vc.titleStr = @"隐私政策";
    vc.urlStr = kGlobalVariables.appConfig.privacy.privacyAgreement;
    vc.needNav = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)agreeAct:(UIButton *)sender {
    sender.selected = !sender.selected;
}

- (IBAction)weiAct:(UIButton *)sender {
    self.loginType = XMLoginTypeWei;
    if (!self.agreeButton.selected) {
        [self showTipView];
        return;
    }
    [kWXApiManager weChatLogin];
}

- (IBAction)qqAct:(UIButton *)sender {
    self.loginType = XMLoginTypeQQ;
    if (!self.agreeButton.selected) {
        [self showTipView];
        return;
    }
    [self qqLogin];
}

- (void)qqLogin {
    NSArray *permissions = @[kOPEN_PERMISSION_GET_USER_INFO, kOPEN_PERMISSION_GET_SIMPLE_USER_INFO];
    _tencentOAuth.authShareType = AuthShareType_QQ;
    _tencentOAuth.authMode = kAuthModeClientSideToken; //授权方式使用Server Side Code
    [_tencentOAuth authorize:permissions];
}

- (void)phoneLogin {
    XMPhoneLoginViewController *vc = [[XMPhoneLoginViewController alloc] initWithNibName:kStringFromClass(XMPhoneLoginViewController) bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - WXManagerDelegate
// 登录回调
- (void)weChatLoginSuccessAuthResp:(SendAuthResp *)resp {
    NSLog(@"用户信息=====%@",resp);
    if (![XMUtil isBlankString:resp.code]) {
        [self thirdLogin:resp.code type:@"weixin"];
    }
}

- (void)weChatLoginCancel {
    NSLog(@"用户取消登录");
}

- (void)weChatLoginFailed:(NSError *)error {
    NSInteger code = error.code;
    NSString *message = error.userInfo[@"message"];
    NSLog(@"错误码==%zd,错误信息===%@",code,message);
}

#pragma mark - TencentLoginDelegate
// 成功后的回调
- (void)tencentDidLogin {
    NSLog(@"授权登录成功");
    //注意，区分两种授权模式：
    //1.Server Side Code Mode:
    //    NSString *code = [_tencentOAuth getServerSideCode];
    //    NSLog(@"Authorization Code：%@", code);
    //2.Client Side Token Mode:
    NSString *token = [_tencentOAuth accessToken];
    NSLog(@"Access Token：%@", token);
    
    if (![XMUtil isBlankString:token]) {
        [self thirdLogin:token type:@"qq"];
    }
}

// 失败后的回调
- (void)tencentDidNotLogin:(BOOL)cancelled {
    NSLog(@"QQ登录失败");
}

// 网络有问题的回调
- (void)tencentDidNotNetWork {
    NSLog(@"QQ登录时网络有问题");
}

- (void)thirdLogin:(NSString *)token type:(NSString *)type {
    [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setObject:[NSString xm_stringNoNullWith:token] forKey:@"token"];
    [dic setObject:[NSString xm_stringNoNullWith:type] forKey:@"driver"];
    [dic setObject:[kMMKV getStringForKey:@"idfa" defaultValue:@""] forKey:@"idfa"];
    [YHNetworkManager.sharedInstance postWithUrl:@"api/auth/codeLogin" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        if (tip.success) {
            [MBProgressHUD hideHUD];
            [self loginSuccess:result];
        }
    }];
}

- (void)loginSuccess:(id)result {
    if (![XMUtil isBlankString:kValueToString([result safe_objectForKey:@"person_id"])]) {
        NSString *person_id = kValueToString([result safe_objectForKey:@"person_id"]);
        NSString *content = [result toString:@"content"];
        if ([XMUtil isBlankString:content]) {
            XMUserModel *model = [XMUserModel mj_objectWithKeyValues:result];
            [XMLoginManager.sharedInstance doLoginResult:model];
        } else {
            XMRegistOneViewController *vc = [[XMRegistOneViewController alloc] initWithNibName:kStringFromClass(XMRegistOneViewController) bundle:nil];
            vc.person_id = person_id;
            vc.content = content;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}


#pragma mark - 一键登录
- (void)oneKeyLogin {
    OLAuthViewModel *viewModel = [OLAuthViewModel new];
    viewModel.backgroundColor = UIColor.whiteColor;
    // -------------- 导航栏设置 -------------------
    viewModel.naviTitle = [[NSAttributedString alloc] initWithString:@""
                                                          attributes:@{NSForegroundColorAttributeName : UIColor.blackColor,
                                                                       NSFontAttributeName : [UIFont boldSystemFontOfSize:18]
                                                                     }];  // 导航栏标题
    viewModel.naviBgColor = UIColor.whiteColor; // 导航栏背景色
    //    viewModel.backgroundImage = kGetImage(@"login_bg");
    viewModel.naviBackImage = [UIImage imageNamed:@"xm_arrow_back"]; // 导航栏返回按钮
    viewModel.backButtonHidden = NO; // 是否隐藏返回按钮，默认不隐藏
    OLRect backButtonRect = {0, 0, 0, 0, 0, 0, {0, 0}}; // 返回按钮偏移、大小设置，偏移量和大小设置值需大于0，否则取默认值，默认可不设置
    viewModel.backButtonRect = backButtonRect;
    
    // -------------- logo设置 -------------------
    viewModel.appLogo = [UIImage imageNamed:@"xm_logo"];  // 自定义logo图片
    OLRect logoRect = {0, 0, 0, 20, 0, 0, {72, 72}}; // logo偏移、大小设置，偏移量和大小设置值需大于0，否则取默认值，默认可不设置，logo大小默认为图片大小
    viewModel.logoRect = logoRect;
    viewModel.logoHidden = NO; // 是否隐藏logo，默认不隐藏
    viewModel.logoCornerRadius = 0; // logo圆角，默认为0
    
    // -------------- 手机号设置 -------------------
    viewModel.phoneNumColor = k1F1F1F; // 颜色
    viewModel.phoneNumFont = [UIFont systemFontOfSize:21]; // 字体
    OLRect phoneNumRect = {0, 0, 0, 0, 0, 0, {0, 0}};  // 手机号偏移设置
    viewModel.phoneNumRect = phoneNumRect;
    
    // -------------- 切换账号设置 -------------------
    //    viewModel.switchButtonColor = UIColor.brownColor; // 切换按钮颜色
    //    viewModel.switchButtonFont = [UIFont systemFontOfSize:15];  // 切换按钮字体
    //    viewModel.switchButtonText = @"自定义切换按钮文案";  // 切换按钮文案
    //    viewModel.switchButtonHidden = YES; // 是否隐藏切换按钮，默认不隐藏
    //    OLRect switchButtonRect = {0, 0, 0, 0, 0, 0, {0, 0}};  // 切换按钮偏移、大小设置，偏移量和大小设置值需大于0，否则取默认值，默认可不设置
    //    viewModel.switchButtonRect = switchButtonRect;
    
    // -------------- 授权登录按钮设置 -------------------
    viewModel.authButtonImages = @[
        [UIImage imageWithColor:kHEX(@"#E74E63")],
        [UIImage imageWithColor:kHEX(@"#E74E63")],
        [UIImage imageWithColor:kHEX(@"#D9243D")]
    ];   // 授权按钮背景图片
    viewModel.authButtonTitle = [[NSAttributedString alloc] initWithString:@"本机号码一键登录"
                                                                attributes:@{NSForegroundColorAttributeName : UIColor.whiteColor,
                                                                             NSFontAttributeName : [UIFont boldSystemFontOfSize:15]
                                                                           }];  // 登录按钮文案
    OLRect authButtonRect = {0, 0, 0, 0, 0, 0, {kScreenWidth - 60, 42}};  // 授权按钮偏移、大小设置，偏移量和大小设置值需大于0，否则取默认值，默认可不设置
    viewModel.authButtonRect = authButtonRect;
    viewModel.authButtonCornerRadius = 21; // 授权按钮圆角，默认为0
    viewModel.clickAuthButtonBlock = ^(void) {  // 点击授权页面登录按钮回调
        NSLog(@"clickAuthButtonBlock");
    };
    
    // -------------- slogan设置 -------------------
    viewModel.sloganTextColor = kHEX(@"959595"); // slogan颜色
    viewModel.sloganTextFont = [UIFont systemFontOfSize:13]; // slogan字体
    OLRect sloganRect = {0, 0, 0, 0, 0, 0, {0, 0}};  // slogan偏移、大小设置，偏移量和大小设置值需大于0，否则取默认值，默认可不设置
    viewModel.sloganRect = sloganRect;
    
    // -------------- 服务条款设置 -------------------
    viewModel.defaultCheckBoxState = NO; // 是否默认选择同意服务条款，默认同意
    viewModel.checkedImage = [UIImage imageNamed:@"xm_agree_s"]; // 复选框选中状态图片
    viewModel.uncheckedImage = [UIImage imageNamed:@"xm_agree_n"]; // 复选框未选中状态图片
    OLRect checkBoxRect = {0, 0, 0, 0, 0, 0, {20, 20}};
    viewModel.checkBoxRect = checkBoxRect; // 复选框尺寸，默认为12*12
    // 隐私条款文字属性
    viewModel.hasQuotationMarkOnCarrierProtocol = YES;
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 1.33;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    paragraphStyle.paragraphSpacing = 0.0;
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.firstLineHeadIndent = 0.0;
    viewModel.termTextColor = k1F1F1F;
    viewModel.privacyTermsAttributes = @{
        NSForegroundColorAttributeName : kAppMainColor,
        NSParagraphStyleAttributeName : paragraphStyle,
        NSFontAttributeName : [UIFont systemFontOfSize:14]
    };
    // 额外自定义服务条款，注意index属性，默认的index为0，SDK会根据index对多条服务条款升序排列，假如想设置服务条款顺序为 自定义服务条款1 默认服务条款 自定义服务条款2，则，只需将自定义服务条款1的index设为-1，自定义服务条款2的index设为1即可
    OLPrivacyTermItem *item1 = [[OLPrivacyTermItem alloc] initWithTitle:@"《用户协议》"
                                                                linkURL:[NSURL URLWithString:[NSString xm_stringNoNullWith:kGlobalVariables.appConfig.privacy.serviceAgreement]]
                                                                  index:0
                                                                  block:^(OLPrivacyTermItem * _Nonnull termItem, UIViewController *controller) {
        //        [self xm_openURL:YHRouterWebCommonPage routerParameters:@{@"url": kGlobalVariables.appConfig.privacy.serviceAgreement, @"title": @"用户协议"} sourceViewController:nil];
        
    }];
    OLPrivacyTermItem *item2 = [[OLPrivacyTermItem alloc] initWithTitle:@"《隐私政策》"
                                                                linkURL:[NSURL URLWithString:@""]
                                                                  index:0
                                                                  block:^(OLPrivacyTermItem * _Nonnull termItem, UIViewController *controller) {
        //        [self xm_openURL:YHRouterWebCommonPage routerParameters:@{@"url": kGlobalVariables.appConfig.privacy.privacyAgreement, @"title": @"隐私政策"} sourceViewController:nil];
    }];
    
    viewModel.additionalPrivacyTerms = @[item1, item2];
    OLRect termsRect = {0, 0, 0, 0, 0, 0, {0, 0}};  // 服务条款偏移、大小设置，偏移量和大小设置值需大于0，否则取默认值，默认可不设置
    viewModel.termsRect = termsRect;
    viewModel.auxiliaryPrivacyWords = @[@"登录即代表您同意", @"和", @"和", @""];   // 条款之外的文案，默认可不设置
    
    viewModel.clickCheckboxBlock = ^(BOOL isChecked) {      // 点击隐私条款前勾选框回调
        NSLog(@"clickCheckboxBlock isChecked: %@", isChecked ? @"YES" : @"NO");
    };
    
    viewModel.termsAlignment = NSTextAlignmentCenter;
    
    // -------------- 服务条款H5页面导航栏设置 -------------------
    viewModel.webNaviBgColor = UIColor.whiteColor;
    viewModel.webNaviHidden = NO;
    // -------------- 授权页面点击登录按钮之后的loading设置 -------------------
    viewModel.loadingViewBlock = ^(UIView * _Nonnull containerView) {
        if ([OneLoginPro isProtocolCheckboxChecked]) {
            UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
            [containerView addSubview:indicatorView];
            indicatorView.center = CGPointMake(containerView.bounds.size.width/2, containerView.bounds.size.height/2);
            [indicatorView startAnimating];
        }
    };
    
    viewModel.stopLoadingViewBlock = ^(UIView * _Nonnull containerView) {
        for (UIView *subview in containerView.subviews) {
            if ([subview isKindOfClass:[UIActivityIndicatorView class]]) {
                [(UIActivityIndicatorView *)subview stopAnimating];
                [subview removeFromSuperview];
                break;
            }
        }
    };
    
    // -------------- 授权页面未勾选服务条款时点击登录按钮的提示 -------------------
    viewModel.notCheckProtocolHint = @"请您先同意服务条款";  // 授权页面未勾选服务条款时点击登录按钮的提示，默认为"请同意服务条款"
    // -------------- Autolayout -------------------
    viewModel.autolayoutBlock = ^(UIView *authView, UIView *authContentView, UIView *authBackgroundImageView, UIView *authNavigationView, UIView *authNavigationContainerView, UIView *authBackButton, UIView *authNavigationTitleView, UIView *authLogoView, UIView *authPhoneView, UIView *authSwitchButton, UIView *authLoginButton, UIView *authSloganView, UIView *authAgreementView, UIView *authCheckbox, UIView *authProtocolView, UIView *authClosePopupButton) {
        // content
        [authContentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(authView);
        }];
        
        // background
        [authBackgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(authContentView);
        }];
        
        // navigation
        [authNavigationView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(authContentView);
            make.height.mas_equalTo(kNavBarAndStatusBarHeight);
        }];
        
        [authNavigationContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(authNavigationView);
        }];
        
        [authBackButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(authNavigationContainerView).offset(20);
            make.centerY.equalTo(authNavigationContainerView).offset(kIPhoneX ? 20 : 10);
            make.size.mas_equalTo(CGSizeMake(20, 20));
        }];
        
        [authNavigationTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(authNavigationContainerView);
            make.centerY.equalTo(authNavigationContainerView).offset(kIPhoneX ? 20 : 10);
            make.size.mas_equalTo(CGSizeMake(100, 40));
        }];
        
        // logo
        [authLogoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(authContentView);
            make.top.equalTo(authNavigationView.mas_bottom).offset(114);
            make.size.mas_equalTo(CGSizeMake(72, 72));
        }];
        
        // phone
        [authPhoneView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(authContentView);
            make.top.equalTo(authLogoView.mas_bottom).offset(23);
            make.size.mas_equalTo(CGSizeMake(200, 20));
        }];
        
        
        // loginbutton
        [authLoginButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(authContentView);
            make.top.equalTo(authPhoneView.mas_bottom).offset(30);
            make.size.mas_equalTo(CGSizeMake(kScreenWidth - 60, 42));
        }];
        
        // slogan
        [authSloganView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(authContentView);
            make.top.equalTo(authLoginButton.mas_bottom).offset(10);
            make.size.mas_equalTo(CGSizeMake(260, 20));
        }];
        
        // agreementview
        [authAgreementView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(authContentView).offset(20);
            make.right.equalTo(authContentView).offset(-20);
            make.bottom.equalTo(authContentView.mas_bottom).offset(-20);
            make.height.mas_equalTo(80);
        }];
        
        [authCheckbox mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(authAgreementView).offset(0);
            make.top.equalTo(authAgreementView).offset(0);
            make.width.height.mas_equalTo(20);
        }];
        
        [authProtocolView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(authCheckbox.mas_right).offset(-5);
            make.right.equalTo(authAgreementView).offset(-10);
            make.height.equalTo(authAgreementView);
            make.top.equalTo(authAgreementView).offset(2);
        }];
        
    };
    
    viewModel.pullAuthVCStyle = OLPullAuthVCStylePush;
    
    // 在SDK内部预取号未成功时，建议加载进度条
    if (![OneLoginPro isPreGetTokenResultValidate]) {
        [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
    }
    // --------------授权页面生命周期回调 -------------------
    XMWeakSelf(self);
    viewModel.viewLifeCycleBlock = ^(NSString *viewLifeCycle, BOOL animated) {
        //        YHStrongSelf(self);
        if ([viewLifeCycle isEqualToString:@"viewDidDisappear:"]) {
            //            self.oneKeyButton.enabled = YES;
        } else if ([viewLifeCycle isEqualToString:@"viewDidLoad"]) {
            [MBProgressHUD hideHUD];
        } else if ([viewLifeCycle isEqualToString:@"viewWillAppear:"]) {
            [MBProgressHUD hideHUD];
        }
    };
    
    [OneLoginPro requestTokenWithViewController:self viewModel:viewModel completion:^(NSDictionary * _Nullable result) {
        XMStrongSelf(self);
        NSLog(@"OneLoginPro requestTokenWithViewController result: %@", result);
        [self finishRequestingToken:result];
        //        self.oneKeyButton.enabled = YES;
    }];
    
}

- (void)finishRequestingToken:(NSDictionary *)result {
    if (result.count > 0 && result[OLStatusKey] && 200 == [result[OLStatusKey] integerValue]) {
        NSString *token = result[OLTokenKey];
        // NSString *appID = result[OLAppIDKey];
        NSString *processID = result[OLProcessIDKey];
        NSString *authcode = result[OLAuthcodeKey];
        [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
        
        [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
        NSMutableDictionary *dic = @{}.mutableCopy;
        [dic setObject:[NSString xm_stringNoNullWith:processID] forKey:@"process"];
        [dic setObject:[NSString xm_stringNoNullWith:authcode] forKey:@"authcode"];
        [dic setObject:[NSString xm_stringNoNullWith:token] forKey:@"token"];
        
        [YHNetworkManager.sharedInstance postWithUrl:@"api/auth/mobileAuthLogin" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
            if (tip.success) {
                [MBProgressHUD hideHUD];
                [self loginSuccess:result];
            }
        }];
        
        //        [[YJXYHSwitch sharedInstance].viewModel oneKeyLoginWithToken:token authcode:authcode process:processID
        //                                                       completeBlock:^(YHResponseTip *_Nonnull tip, YJXYHUstomer *_Nullable result) {
        //
        //            if (tip.success) {
        //                [MBProgressHUD hideHUD];
        //                [[YJXYHSwitch sharedInstance] doLoginResult:result];
        //            }
        //        }];
        
    } else {
        if (result[OLErrCodeKey] && [result[OLErrCodeKey] isEqual:OLErrorCode_20302]) { // 点击授权页面返回按钮退出授权页面
        } else if (result[OLErrCodeKey] && [result[OLErrCodeKey] isEqual:OLErrorCode_20303]) { // 点击授权页面切换账号按钮
            [MBProgressHUD showHUDMsg:@"切换账号" toView:nil];
            [OneLoginPro dismissAuthViewController:nil];
        } else {
            [MBProgressHUD showHUDMsg:@"登录失败" toView:nil];
            [OneLoginPro dismissAuthViewController:nil];
        }
    }
}


- (void)showTipView {
    if ([GKCover hasCover]) {
        XMWeakSelf(self);
        [GKCover hideCoverWithHideBlock:^{
            XMStrongSelf(self);
            [self coverTipView];
        }];
    } else {
        [self coverTipView];
    }
}


- (void)coverTipView {
    [GKCover coverFrom:self.view contentView:self.tipView style:(GKCoverStyleTranslucent) showStyle:(GKCoverShowStyleCenter) showAnimStyle:(GKCoverShowAnimStyleNone) hideAnimStyle:(GKCoverHideAnimStyleNone) notClick:YES];
}

- (XMLoginPrivacyTipView *)tipView {
    if (_tipView == nil) {
        _tipView = [XMLoginPrivacyTipView viewFromXib];
        _tipView.size = CGSizeMake(295, 177);
        [_tipView setupProtocolAndPrivacy];
    }
    return _tipView;
}

@end
